Lade Packages

library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.0 ──
## ✓ ggplot2 3.3.2     ✓ purrr   0.3.4
## ✓ tibble  3.0.6     ✓ dplyr   1.0.4
## ✓ tidyr   1.1.2     ✓ stringr 1.4.0
## ✓ readr   1.4.0     ✓ forcats 0.5.0
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(palmerpenguins)

Daten kennenlernen

Penguins

glimpse(penguins)
## Rows: 344
## Columns: 8
## $ species           <fct> Adelie, Adelie, Adelie, Adelie, Adelie, Adelie, Ade…
## $ island            <fct> Torgersen, Torgersen, Torgersen, Torgersen, Torgers…
## $ bill_length_mm    <dbl> 39.1, 39.5, 40.3, NA, 36.7, 39.3, 38.9, 39.2, 34.1,…
## $ bill_depth_mm     <dbl> 18.7, 17.4, 18.0, NA, 19.3, 20.6, 17.8, 19.6, 18.1,…
## $ flipper_length_mm <int> 181, 186, 195, NA, 193, 190, 181, 195, 193, 190, 18…
## $ body_mass_g       <int> 3750, 3800, 3250, NA, 3450, 3650, 3625, 4675, 3475,…
## $ sex               <fct> male, female, female, NA, female, male, female, mal…
## $ year              <int> 2007, 2007, 2007, 2007, 2007, 2007, 2007, 2007, 200…
str(penguins)
## tibble [344 × 8] (S3: tbl_df/tbl/data.frame)
##  $ species          : Factor w/ 3 levels "Adelie","Chinstrap",..: 1 1 1 1 1 1 1 1 1 1 ...
##  $ island           : Factor w/ 3 levels "Biscoe","Dream",..: 3 3 3 3 3 3 3 3 3 3 ...
##  $ bill_length_mm   : num [1:344] 39.1 39.5 40.3 NA 36.7 39.3 38.9 39.2 34.1 42 ...
##  $ bill_depth_mm    : num [1:344] 18.7 17.4 18 NA 19.3 20.6 17.8 19.6 18.1 20.2 ...
##  $ flipper_length_mm: int [1:344] 181 186 195 NA 193 190 181 195 193 190 ...
##  $ body_mass_g      : int [1:344] 3750 3800 3250 NA 3450 3650 3625 4675 3475 4250 ...
##  $ sex              : Factor w/ 2 levels "female","male": 2 1 1 NA 1 2 1 2 NA NA ...
##  $ year             : int [1:344] 2007 2007 2007 2007 2007 2007 2007 2007 2007 2007 ...
nrow(penguins)
## [1] 344
ncol(penguins)
## [1] 8
penguins
## # A tibble: 344 x 8
##    species island bill_length_mm bill_depth_mm flipper_length_… body_mass_g
##    <fct>   <fct>           <dbl>         <dbl>            <int>       <int>
##  1 Adelie  Torge…           39.1          18.7              181        3750
##  2 Adelie  Torge…           39.5          17.4              186        3800
##  3 Adelie  Torge…           40.3          18                195        3250
##  4 Adelie  Torge…           NA            NA                 NA          NA
##  5 Adelie  Torge…           36.7          19.3              193        3450
##  6 Adelie  Torge…           39.3          20.6              190        3650
##  7 Adelie  Torge…           38.9          17.8              181        3625
##  8 Adelie  Torge…           39.2          19.6              195        4675
##  9 Adelie  Torge…           34.1          18.1              193        3475
## 10 Adelie  Torge…           42            20.2              190        4250
## # … with 334 more rows, and 2 more variables: sex <fct>, year <int>

ggplot2

Argumente

ggplot(data = penguins, 
       mapping = aes(x = bill_depth_mm,
                     y = bill_length_mm,
                     colour = species)) +
  geom_point() 
## Warning: Removed 2 rows containing missing values (geom_point).

Visuelle Eigenschaften (Aesthetics) Optionen

  • colour
  • shape
  • size
  • alpha (Transparenz)
ggplot(penguins, 
       aes(x = bill_length_mm,
           y = bill_depth_mm,
           colour = species, 
           shape = species,
           size = body_mass_g,
           alpha = flipper_length_mm)) +
  geom_point() 
## Warning: Removed 2 rows containing missing values (geom_point).

Mapping (einer visuelle Eigenschaft) vs. Setting (einer visuellen Eigenschaft)

ggplot(penguins, 
       aes(x = bill_length_mm,
           y = bill_depth_mm,
           size = body_mass_g,
           alpha = flipper_length_mm)) +
  geom_point()
## Warning: Removed 2 rows containing missing values (geom_point).

ggplot(penguins, 
       aes(x = bill_length_mm,
           y = bill_depth_mm)) +
  geom_point(size = 5, alpha = 0.5)
## Warning: Removed 2 rows containing missing values (geom_point).

Faceting

  • facet_grid()
ggplot(penguins, 
       aes(x = bill_length_mm,
           y = bill_depth_mm)) +
  geom_point() +
  facet_grid(species ~ island)
## Warning: Removed 2 rows containing missing values (geom_point).

Übung 1

In den nächsten Code-chunks habe ich bereits Code für dich vorbereitet. Beschreibe was in den Plots vorgeht und wie der Code in Beziehung zum Output steht.

Lösche diesen Text und beschreibe was in den Plots und im Code vorgeht

ggplot(penguins, 
       aes(x = bill_depth_mm, 
           y = bill_length_mm)) + 
  geom_point() +
  facet_grid(species ~ sex)
## Warning: Removed 2 rows containing missing values (geom_point).

Lösche diesen Text und beschreibe was in den Plots und im Code vorgeht

ggplot(penguins, 
       aes(x = bill_depth_mm, 
           y = bill_length_mm)) + 
  geom_point() +
  facet_grid(sex ~ species)
## Warning: Removed 2 rows containing missing values (geom_point).

Lösche diesen Text und beschreibe was in den Plots und im Code vorgeht

ggplot(penguins, 
       aes(x = bill_depth_mm, 
           y = bill_length_mm)) + 
  geom_point() +
  facet_wrap(~ species)
## Warning: Removed 2 rows containing missing values (geom_point).

Lösche diesen Text und beschreibe was in den Plots und im Code vorgeht

ggplot(penguins, 
       aes(x = bill_depth_mm,
           y = bill_length_mm)) + 
  geom_point() +
  facet_grid(. ~ species)
## Warning: Removed 2 rows containing missing values (geom_point).

Lösche diesen Text und beschreibe was in den Plots und im Code vorgeht

ggplot(penguins, 
       aes(x = bill_depth_mm, 
           y = bill_length_mm)) + 
  geom_point() +
  facet_wrap(~ species)
## Warning: Removed 2 rows containing missing values (geom_point).

Lösche diesen Text und beschreibe was in den Plots und im Code vorgeht

ggplot(penguins, 
       aes(x = bill_depth_mm, 
           y = bill_length_mm)) + 
  geom_point() +
  facet_wrap(~ species, ncol = 2)
## Warning: Removed 2 rows containing missing values (geom_point).

Skalierung

  • scale_color_viridis
ggplot(penguins, 
       aes(x = bill_length_mm,
           y = bill_depth_mm,
           colour = species)) +
  geom_point() +
  scale_color_viridis_d()
## Warning: Removed 2 rows containing missing values (geom_point).

  • scale_color_brewer
ggplot(penguins, 
       aes(x = bill_length_mm,
           y = bill_depth_mm,
           colour = species)) +
  geom_point() +
  scale_color_brewer(type = "qualitative", palette = "Set1")
## Warning: Removed 2 rows containing missing values (geom_point).

  • scale_color_manual
# Farben mit Namen

ggplot(penguins, 
       aes(x = bill_length_mm,
           y = bill_depth_mm,
           colour = species)) +
  geom_point() +
  scale_color_manual(values = c("red", "blue", "green"))
## Warning: Removed 2 rows containing missing values (geom_point).

# Hex colors

ggplot(penguins, 
       aes(x = bill_length_mm,
           y = bill_depth_mm,
           colour = species)) +
  geom_point() +
  scale_color_manual(values = c('#9EB0FFFF', '#180B09FF', '#FFACACFF'))
## Warning: Removed 2 rows containing missing values (geom_point).

  • scale_x_continuous
ggplot(penguins, 
       aes(x = bill_length_mm,
           y = bill_depth_mm,
           colour = species)) +
  geom_point() +
  scale_color_viridis_d() +
  scale_x_continuous(breaks = seq(0, 70, 5), limits = c(0, 70)) +
  scale_y_continuous(breaks = seq(0, 30, 2.5), limits = c(0, 30))
## Warning: Removed 2 rows containing missing values (geom_point).

seq(from = 0, to = 70, by = 5)
##  [1]  0  5 10 15 20 25 30 35 40 45 50 55 60 65 70
c(0, 70)
## [1]  0 70

Übung 2

Fülle die Lücken aus

  • Nutze den penguins Datensatz
  • Ordne die Variable ‘island’ der x-Achse und die Variable ‘species’ einer Farbskalierung mit ‘island’ auf der x-Achse
  • Nutze Faceting und lege die Variable ‘sex’ auf die Reihen und die Variable ‘species’ auf die Spalten
  • Nutze die colorbrewer Funktion um die Farben anzupassen
  • Entferne die Legende (Tipp: Nutze die Suchmaschine deiner Wahl “How to remove the legend in ggplot2 tidyverse”)
ggplot(___, 
       ___(x = ___,
           ___ = species)) +
  geom____() +
  facet_grid(___ ~ ___) +
  scale_fill_brewer(type = "qual", palette = "Set2") +
  ___(___ = "none")
ggplot(penguins, 
       aes(x = island,
           fill = species)) +
  geom_bar() +
  facet_grid(sex ~ species) +
  scale_fill_brewer(type = "qual", palette = "Set2") +
  theme(legend.position =  "none")

Git commit / push

Du kannst jederzeit einen Git commit durchführen und deine Änderungen auf GitHub pushen. Mache dies spätestens am Ende dieses Praktikums nachdem du sichergestellt hast, dass deine R Markdown Datei zu einer HTML Datei rendert.

Hinweis: Der Begriff Rendern (Engl. to render; zu Deutsch: Bildsynthese) bezeichnet die Erstellung einer Grafik aus Rohdaten (wie z. B. Geoinformationen) oder einer Skizze. Quelle: Wikipedia